
package com.rx.mgt.dao;  

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.rx.mgt.dao.base.ManagementHibernateDaoBase;
import com.rx.mgt.domain.UserMailInfo;
import com.rx.mgt.po.UserMailInfoCountBo;
import com.rx.mgt.po.UsersBo;

/** 
 * ClassName:UserMailInfoDao 
 * Function :
 * Reason   : 
 * Date     :Feb 7, 2017 2:53:15 PM 
 * @author   luolei 
 * @since    JDK 1.6
 */
@Repository
public class UserMailInfoDao  extends ManagementHibernateDaoBase<UserMailInfo, Long>{
    @Transactional(readOnly=true)
    public List<UserMailInfoCountBo> getUserMailInfoCount(UsersBo usersBo){
        String hql= "select count(au.id) as msgCount,sum(au.size) as msgSize from UserMailInfo au where 1=1  ";
        if(StringUtils.isNotBlank(usersBo.getStartDate())){
            hql=hql +" and  unix_timestamp(au.create_time)>unix_timestamp(:startTime)";
        }
        if(StringUtils.isNotBlank(usersBo.getType())){
            hql=hql +" and au.type=:type";
        }
        Query query = template.getSessionFactory().getCurrentSession().createQuery(hql);
        if(StringUtils.isNotBlank(usersBo.getStartDate())){
            query.setParameter("startTime", usersBo.getStartDate());
        }
        if(StringUtils.isNotBlank(usersBo.getType())){
            query.setParameter("type", usersBo.getType());
        }
        query.setResultTransformer(new AliasToBeanResultTransformer(UserMailInfoCountBo.class));
        return query.list();
    }

}
  